/**
 * @desc    图片相关服务封装
 * @author  刘贺[of2220]
 * @company qianmi.com
 * @Date    2017/2/10
 */
'use strict';
import * as uuid from 'uuid';
import RelatedImgInfoModel from '../dao/relatedImgInfo/related-img-info-model';

export default class RelatedImgInfoService {
  /******************************** 目录相关 start**************************************/
  /**
     * 新建一个目录
     * @param id    //按照规则生成
     * @param name
     * @param pid   '-1' 为一级目录
     */
  static createNewCate({ id, name, pid = '-1' }) {
    return new Promise((resolve, reject) => {
      RelatedImgInfoModel.create({ id, name, pid, isCate: true }, function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
  }

  /**
     * 删除目录 及其子节点和所有图片信息
     * @param id    当前目录的ID 1001-1001   
     */
  static deleteCate({ id }) {
    let reg = new RegExp('^' + id + '*', 'i');
    //删除当前目录 目录下的图片 目录下的子目录 子目录的图片
    return new Promise((resolve, reject) => {
      RelatedImgInfoModel.remove(
        {
          $or: [{ id: id, isCate: true }, { id: reg, isCate: true }, { pid: reg, isCate: false }]
        },
        function(err, data) {
          err ? reject(err) : resolve(data);
        }
      );
    });
  }

  /**
     * 修改目录名称
     * @param id
     * @param name
     * @returns {Promise}
     */
  static updateCateName({ id, name }) {
    return new Promise((resolve, reject) => {
      RelatedImgInfoModel.findOneAndUpdate({ id, isCate: true }, { $set: { name } }, function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
  }

  /**
     * 查询所有的目录
     * @returns {Promise}
     */
  static getCategoryTree() {
    return new Promise((resolve, reject) => {
      RelatedImgInfoModel.find({ isCate: true }).sort({ _id: -1 }).exec(function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
  }

  /**
     * 查询当前目录下的 子目录 和 图片
     * @param id
     * @param pageNum
     * @param pageSize
     * @returns {Promise}
     */
  static async getCateTreeAndImgByPid({ id, pageNum, pageSize }) {
    let data = await new Promise((resolve, reject) => {
      RelatedImgInfoModel.aggregate([
        { $match: { pid: id } },
        { $sort: { isCate: -1, _id: -1 } },
        { $skip: pageNum * pageSize },
        { $limit: pageSize }
      ]).exec(function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
    let count = await new Promise((resolve, reject) => {
      RelatedImgInfoModel.find({ pid: id }).count().exec(function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
    return { data, count };
  }

  /******************************** 图片相关 start**************************************/
  /**
     * 保存一张图片
     * @param pid
     * @param name
     * @param src
     * @param des
     * @param keyword
     * @param fileSize
     * @param imgSize
     * @returns {Promise}
     */
  static saveImage({ pid, name, src, des = '', keyword = [], fileSize = '', imgSize = '' }) {
    return new Promise((resolve, reject) => {
      new RelatedImgInfoModel({
        id: uuid.v1(),
        pid,
        name,
        src,
        isCate: false,
        des,
        keyword,
        fileSize,
        imgSize,
        uploadTime: Date.now().toString()
      }).save(function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
  }

  /**
     * 删除图片
     * @param id
     * @returns {Promise}
     */
  static deleteImage({ id }) {
    return new Promise((resolve, reject) => {
      RelatedImgInfoModel.findOneAndRemove({ id, isCate: false }, function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
  }

  /**
     * 更新图片信息
     * @param id
     * @param imgInfo       Object
     * @returns {Promise}
     */
  static updateImageInfo({ id, imgInfo = {} }) {
    return new Promise((resolve, reject) => {
      RelatedImgInfoModel.findOneAndUpdate({ id }, { $set: imgInfo }, function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
  }

  /**
     * 分页查询当前目录下的图片列表
     * @param pid
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @returns {Promise}
     */
  static async getImageList({ pid, pageNum, pageSize, keyword }) {
    let keywordTarget = keyword.map((val, index) => {
      return { keyword: val };
    });
    let target = { pid, isCate: false };
    if (keywordTarget.length > 0) target['$or'] = keywordTarget;
    let data = await new Promise((resolve, reject) => {
      RelatedImgInfoModel.aggregate([
        { $match: target },
        { $skip: pageNum * pageSize },
        { $limit: pageSize }
      ]).exec(function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
    let count = await new Promise((resolve, reject) => {
      RelatedImgInfoModel.count(target).exec(function(err, data) {
        err ? reject(err) : resolve(data);
      });
    });
    return { data, count };
  }
}
